package cn.baiweigang.uu.task;

import cn.baiweigang.uu.GlobalContext;
import cn.baiweigang.uu.db.dataobj.ConfigDo;
import cn.baiweigang.uu.db.repository.ConfigRepository;
import cn.baiweigang.uu.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TaskStart implements CommandLineRunner {
    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    public ConfigRepository configRepository;

    @Override
    public void run(String... args) throws Exception {
        LogUtil.logInfo("程序启动 初始化信息");
        //加载db配置项
        List<ConfigDo> configDoList=configRepository.queryAll();
        if (configDoList!=null){
            for (int i=0;i<configDoList.size();i++){
                GlobalContext.dbConfigCache.put(configDoList.get(i).getConfigKey(),
                        configDoList.get(i).getConfigValue());
            }
        }
    }



    private Object getBeanByClassName(String className) {
        className = className.substring(0, 1).toLowerCase() + className.substring(1);
        return applicationContext.getBean(className);
    }
}
